<?php
/**
 * 菜单管理
 * @author Administrator
 */
class menu extends admin{
    public function __construct(){
        parent::__construct();
    }
    
    
    public function index(){
		$where = 'where 1=1 ';
		if(isset($_GET['title'])&&$_GET['title']!=''){
		    $title = trim($_GET['title']);
		    $where.=' and title like "%'.$title.'%"';
		}
       $list=db()->getAll('select id,pid as parentid,is_menu,path,title as name,sort from x_menu '.$where.' order by pid ASC');
       if($list){
           $tree = new Tree();
           $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
           $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
           foreach($list AS $k=>$v){
                   if($v['is_menu']==0){
                       $is_menu = '否';
                   }
                   if($v['is_menu']==1){
                       $is_menu = '是';
                   }
                   $list[$k]['is_menu'] = $is_menu;
               }
           $tree->init($list);
           
           $html=<<<html
 <tr>
     <td><input type=\"input\" name=\"sort[]\" value=\"\$sort\" style=\"width:40px\"></td>
     <td style=\"text-align:left !important;\">\$spacer \$name</td>
     <td>\$is_menu</td>
     <td>\$path</td>
     <td>
         <a _href=\"add?id=\$id\" title=\"添加:\$name的子菜单\" onclick=\"openLayer(this,'50%','100%')\" class=\"layui-btn layui-btn-small\">添加子菜单</a>
         <a _href=\"edit?id=\$id\" title=\"编辑:\$name\" onclick=\"openLayer(this,'50%','100%')\" class=\"layui-btn layui-btn-small\">编辑</a>
         <a _href=\"delete?id=\$id\" title=\"确定删除菜单：\$name\" class=\"layui-btn layui-btn-small layui-btn-danger ajax_del_btn\">删除</a>
     </td>
 </tr>
html;
           $str = $tree->get_tree($list[0]['parentid'],$html);
       }
      else $str='';
      
       $tpl=$this->Template();
       $tpl->assign('str',$str);
       $tpl->display(); 
    }
    
    
    public function add(){
        if(IS_AJAX){  
			$data['pid']=(int)$_POST['pid'];
            $data['title'] = trim($_POST['title']);
            $data['m'] = $_POST['m_name'];
            $data['c'] = $_POST['c_name'];
            $data['a'] = $_POST['a_name'];
            $data['path']    = $data['m'].'/'.$data['c'].'/'.$data['a'];
            $data['icon']     = trim($_POST['icon']);
            $data['is_menu'] = $_POST['is_menu'];
			$plevel = db()->getOne("select level from x_menu where id=".$data['pid']);
			$data['level'] = $plevel+1;
            $insert_id=db()->insert('x_menu',$data);
            if($insert_id) $this->ajaxReturn(array('status'=>'y','info'=>'添加成功'));
            else $this->ajaxReturn(array('status'=>'n','info'=>'添加失败'.db()->getError())); 
        }else{
            if(isset($_GET['id'])&&(int)$_GET['id']>0)  {
                $id=(int)$_GET['id'];
            }
            else $id=0;
            $list=db()->getAll('select id,pid as parentid,title as name from x_menu');
            $tree = new Tree();
            $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
            $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
             
            $tree->init($list);
            $html="<option value=\$id \$selected>\$spacer \$name</option>";
             
            $str = $tree->get_tree(0,$html,$id);
            $tpl=$this->Template();
            $tpl->assign('str',$str);
            $tpl->display();
        }
        
    }
    
   public function edit(){
        if(IS_AJAX){
            $data['id']=(int)$_POST['id'];
			$data['pid']=(int)$_POST['pid'];
            $data['title'] = trim($_POST['title']);
            $data['m'] = $_POST['m_name'];
            $data['c'] = $_POST['c_name'];
            $data['a'] = $_POST['a_name'];
             $data['path']    = $data['m'].'/'.$data['c'].'/'.$data['a'];
            $data['icon']     = trim($_POST['icon']);
            $data['is_menu'] = (int)$_POST['is_menu'];
			$plevel = db()->getOne("select level from x_menu where id=".$data['pid']);
			$data['level'] = $plevel+1;
            $rs=db()->update('x_menu',$data,'id='.$data['id']);
            if($rs!==false) {
                $userData = array('userId'=>$_COOKIE[userId],'userType'=>$_COOKIE[userType],'username'=>$_COOKIE[userName]);
                $logs = json_encode($data);
                $infoid = $data[id];
                $method = 'update';
                $mid = 35;
                $remark = '修改';
                insertlogs($userData,$logs,$infoid,$method,$mid,$remark);
                $this->ajaxReturn(array('status'=>'y','info'=>'修改成功'));
            }
            else $this->ajaxReturn(array('status'=>'n','info'=>'修改失败'));  
        }else{
			$id=(int)$_GET['id'];
			$vo=db()->getRow('select * from x_menu where id='.$id);
            $list=db()->getAll('select id,pid as parentid,title as name from x_menu where status=1');
            $tree = new Tree();
            $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
            $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
             
            $tree->init($list);
            $html="<option value=\$id \$selected>\$spacer \$name</option>";
             
            $str = $tree->get_tree(0,$html,$vo['pid']);
            
            $tpl=$this->Template();
            $tpl->assign('vo',$vo);
            $tpl->assign('str',$str);
            $tpl->display();
        }
    }
    
    public function del(){
        if(IS_AJAX){
        
        }
    }
}